/**
 * WebGPU Command Serialization Protocol
 * 参考微信小游戏Metal方案的命令序列化设计
 */

// 命令类型枚举
const CommandType = {
    // Device & Adapter
    CREATE_DEVICE: 0x01,
    DESTROY_DEVICE: 0x02,
    
    // Buffer Operations
    CREATE_BUFFER: 0x10,
    DESTROY_BUFFER: 0x11,
    WRITE_BUFFER: 0x12,
    COPY_BUFFER: 0x13,
    
    // Texture Operations
    CREATE_TEXTURE: 0x20,
    DESTROY_TEXTURE: 0x21,
    WRITE_TEXTURE: 0x22,
    COPY_TEXTURE: 0x23,
    
    // Render Pipeline
    CREATE_RENDER_PIPELINE: 0x30,
    DESTROY_RENDER_PIPELINE: 0x31,
    CREATE_SHADER_MODULE: 0x32,
    DESTROY_SHADER_MODULE: 0x33,
    
    // Bind Group & Layout
    CREATE_BIND_GROUP_LAYOUT: 0x34,
    DESTROY_BIND_GROUP_LAYOUT: 0x35,
    CREATE_BIND_GROUP: 0x36,
    DESTROY_BIND_GROUP: 0x37,
    CREATE_PIPELINE_LAYOUT: 0x38,
    DESTROY_PIPELINE_LAYOUT: 0x39,

    // Texture View & Sampler
    CREATE_TEXTURE_VIEW: 0x3A,
    DESTROY_TEXTURE_VIEW: 0x3B,
    CREATE_SAMPLER: 0x3C,
    DESTROY_SAMPLER: 0x3D,

    // Compute Pipeline
    CREATE_COMPUTE_PIPELINE: 0x40,
    DESTROY_COMPUTE_PIPELINE: 0x41,

    // Command Encoder
    CREATE_COMMAND_ENCODER: 0x50,
    FINISH_COMMAND_ENCODER: 0x51,
    
    // Render Pass
    BEGIN_RENDER_PASS: 0x60,
    END_RENDER_PASS: 0x61,
    SET_PIPELINE: 0x62,
    SET_VERTEX_BUFFER: 0x63,
    SET_INDEX_BUFFER: 0x64,
    SET_BIND_GROUP: 0x65,
    DRAW: 0x66,
    DRAW_INDEXED: 0x67,
    SET_VIEWPORT: 0x68,
    SET_SCISSOR_RECT: 0x69,
    SET_BLEND_CONSTANT: 0x6A,
    SET_STENCIL_REFERENCE: 0x6B,
    
    // Compute Pass
    BEGIN_COMPUTE_PASS: 0x70,
    END_COMPUTE_PASS: 0x71,
    DISPATCH: 0x72,

    // Render Bundle
    CREATE_RENDER_BUNDLE_ENCODER: 0x73,
    FINISH_RENDER_BUNDLE_ENCODER: 0x74,
    EXECUTE_BUNDLES: 0x75,

    // Render Bundle Commands (inside bundle)
    BUNDLE_SET_PIPELINE: 0x80,
    BUNDLE_SET_VERTEX_BUFFER: 0x81,
    BUNDLE_SET_INDEX_BUFFER: 0x82,
    BUNDLE_SET_BIND_GROUP: 0x83,
    BUNDLE_DRAW: 0x84,
    BUNDLE_DRAW_INDEXED: 0x85,
    BUNDLE_SET_VIEWPORT: 0x86,
    BUNDLE_SET_SCISSOR_RECT: 0x87,
    BUNDLE_SET_BLEND_CONSTANT: 0x88,
    BUNDLE_SET_STENCIL_REFERENCE: 0x89,

    // Queue Operations
    SUBMIT: 0x90,
    
    // Batch Control
    BATCH_START: 0xF0,
    BATCH_END: 0xF1,
    FLUSH: 0xFF
};

// 数据类型定义
const DataType = {
    UINT8: 1,
    UINT16: 2,
    UINT32: 4,
    FLOAT32: 4,
    STRING: 0, // 可变长度
    BUFFER: 0  // 可变长度
};

// 命令头部结构 (8字节)
const COMMAND_HEADER_SIZE = 8;
class CommandHeader {
    constructor(type, size, id = 0, flags = 0) {
        this.type = type;     // uint8 - 命令类型
        this.flags = flags;   // uint8 - 标志位
        this.id = id;         // uint16 - 资源ID
        this.size = size;     // uint32 - 命令总大小(包含头部)
    }
    
    serialize(buffer, offset) {
        const view = new DataView(buffer);
        view.setUint8(offset, this.type);
        view.setUint8(offset + 1, this.flags);
        view.setUint16(offset + 2, this.id, true);
        view.setUint32(offset + 4, this.size, true);
        return offset + COMMAND_HEADER_SIZE;
    }
}

// 字符串序列化辅助函数
function serializeString(buffer, offset, str) {
    const encoder = new TextEncoder();
    const bytes = encoder.encode(str);
    const view = new DataView(buffer);
    view.setUint32(offset, bytes.length, true);
    new Uint8Array(buffer, offset + 4, bytes.length).set(bytes);
    return offset + 4 + bytes.length;
}

function deserializeString(buffer, offset) {
    const view = new DataView(buffer);
    const length = view.getUint32(offset, true);
    const decoder = new TextDecoder();
    const str = decoder.decode(new Uint8Array(buffer, offset + 4, length));
    return { value: str, nextOffset: offset + 4 + length };
}

// 导出模块
if (typeof module !== 'undefined' && module.exports) {
    module.exports = {
        CommandType,
        DataType,
        COMMAND_HEADER_SIZE,
        CommandHeader,
        serializeString,
        deserializeString
    };
}